<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/7/22
 * Time: 17:23
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Staffmember extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $user_name = input('user_name');
            $staff_status = input('staff_status');
            $sc_name = input('sc_name');
            $staff_soldier_type = input('staff_soldier_type');
            $where = [];
            $where['staff_type'] = 2; // 成员申请
            //用户名称
            if(isset($user_name) && $user_name != ''){
                $where['staff_name'] = ['like', '%' . $user_name . '%'];
            }
            //审核状态
            if(isset($staff_status) && $staff_status != ''){
                $where['staff_status'] = $staff_status;
            }
            //学校名称
            if(isset($sc_name) && $sc_name != ''){
                $where['sc_name'] = ['like', '%' . $sc_name . '%'];
            }
            //类型
            if(isset($staff_soldier_type) && $staff_soldier_type != ''){
                $where['staff_soldier_type'] = $staff_soldier_type;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            // 获取总条数
            $count = Db::table('up_staff')
                ->join('up_u_user', 'user_id = staff_user_id', 'left')
                ->join('up_school', 'sc_id = staff_school_id', 'left')
                ->join('up_assn', 'staff_assn_id = assn_id', 'left')
                ->where($where)
                ->count();
            $lists = Db::table('up_staff')
                ->join('up_u_user', 'user_id = staff_user_id', 'left')
                ->join('up_school', 'sc_id = staff_school_id', 'left')
                ->join('up_assn', 'staff_assn_id = assn_id', 'left')
                ->field('staff_id, staff_status, staff_name, sc_name, staff_insert_time, 
                staff_soldier_type, assn_name')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('staff_insert_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 申请详情
     * @return mixed
     */
    public function staffDetail(){
        $staff_id = input('staff_id');
        $detail = Db::table('up_staff')
            ->join('__U_USER__', 'staff_user_id = user_id', 'left')
            ->join('__SCHOOL__', 'sc_id = staff_school_id', 'left')
            ->join('__POLITICS__', 'politics_id = staff_politics', 'left')
            ->field('staff_soldier_type, staff_insert_time, staff_status, sc_name, staff_assn_name, 
            staff_user_card, politics_name, staff_assn_duty, staff_in_assn_time, staff_old_troops, 
            staff_sign, staff_sign_time, staff_gender, staff_birthday, staff_faculty, staff_specilay, 
            staff_grade, staff_member_code, staff_reason, staff_name, staff_phone, staff_id')
            ->where('staff_id', $staff_id)
            ->find();
        
        $this->assign([
            'detail' => $detail
        ]);
        return $this->fetch('staffmember/staffdetail');
    }
    
    /**
     * 审核通过
     * @return \think\response\Json
     */
    public function staffAllow(){
        $staff_id = input('id');
        // 判断同类型，同学校，有没有审核通过的记录
        $staff_info = Db::table('up_staff')->where('staff_id', $staff_id)->find();
        $is_have = Db::table('up_staff')
            ->where('staff_type', $staff_info['staff_type'])
            ->where('staff_status', 2)
            ->where('staff_id', 'NEQ', $staff_id)
            ->where('staff_school_id', $staff_info['staff_school_id'])
            ->find();
        if($is_have){
            return json(['code' => 100, 'msg' => '已有审核成功的同类型同学校记录']);
        }
        
        //============== 审核通过，计算高自盟代码  学校代码+年月各两位+B
        // 学校代码
        $school_info = Db::table('up_staff')
            ->where('staff_id', $staff_id)
            ->field('staff_school_id')
            ->find();
        
        $school_code = Db::table('up_school')
            ->where('sc_id', $school_info['staff_school_id'])
            ->value('sc_code');
        
        // 年+月
        $date = date('Ym');
        $date_str = substr($date, 2, 4);
        
        $staff_coach_code = $school_code . $date_str . 'B';
        
        // 更新认证表
        $update = [
            'staff_status' => 2,
            'staff_coach_code' => $staff_coach_code
        ];
        $res = Db::table('up_staff')->where('staff_id', $staff_id)->update($update);
        
        if($res){
            //TODO:推送
//            $user_id = Db::table('up_staff')->where('staff_id', $staff_id)->value('staff_user_id');
//            $list = [];//需要推送的用户数组
//            $send_user_id = (string)$user_id;
//            array_push($list, $send_user_id);
//            try{
//                $this->Jpush(1, $list, '您提交的认证通过审核啦', ['role_user_id' => $user_id, 'status' => 1, 'send_type' => 13]);
//            }catch(\Exception $e){
//            }
            return json(['code' => 200, 'msg' => '审核成功']);
        }else{
            return json(['code' => 100, 'msg' => '审核失败']);
        }
    }
    
    /**
     * 审核驳回
     * @return mixed|\think\response\Json
     */
    public function staffRefuse(){
        if(Request::instance()->isPost()){
            $id = input('staff_id');
            $staff_reason = input('staff_reason');
//            $length = mb_strlen($staff_status, 'UTF8');
//            if($length > 30){
//                return json(['code' => 100, 'msg' => '长度不可超过30个字']);
//            }
            $update = [
                'staff_status' => 3,
                'staff_reason' => $staff_reason,
            ];
            $res = Db::table('up_staff')->where('staff_id', $id)->update($update);
            if($res){
//                //TODO:推送
//                $user_id = Db::table('up_staff')->where('staff_id', $id)->value('staff_user_id');
//                $list = [];//需要推送的用户数组
//                $send_user_id = (string)$user_id;
//                array_push($list, $send_user_id);
//                try{
//                    $this->Jpush(1, $list, '您提交的认证未通过审核', ['role_user_id' => $user_id, 'status' => 2, 'send_type' => 13]);
//                }catch(\Exception $e){
//                }
                return json(['code' => 200, 'msg' => '审核成功']);
            }else{
                return json(['code' => 100, 'msg' => '审核失败']);
            }
        }
        $staff_id = input('staff_id');
        $this->assign('staff_id', $staff_id);
        return $this->fetch('staffmember/staffrefuse');
    }
    
    /**
     * 删除提交
     * @return \think\response\Json
     */
    public function staffDel(){
        $staff_id = input('id');
        $res = Db::table('up_staff')->where('staff_id', $staff_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}